class Solution
{
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> subsets(vector<int>& nums)
    {
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        ret.push_back(path);
        for (int i = pos; i < nums.size(); i++)
        {
            path.push_back(nums[i]);
            dfs(nums, i + 1);
            path.pop_back();
        }
    }
};
class Solution
{
    vector<bool> IsUse;
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> permute(vector<int>& nums)
    {
        int n = nums.size();
        IsUse.resize(n);
        dfs(nums);
        return ret;
    }
    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (!IsUse[i])
            {
                path.push_back(nums[i]);
                IsUse[i] = true;
                dfs(nums);
                path.pop_back();
                IsUse[i] = false;
            }
        }
    }
};
class Solution
{
    vector<string> ret;
public:
    vector<string> binaryTreePaths(TreeNode* root)
    {
        dfs(root, "");
        return ret;
    }
    void dfs(TreeNode* root, string tmp)
    {
        tmp += to_string(root->val);
        tmp += "->";
        if (root->left == nullptr && root->right == nullptr)
        {
            tmp.pop_back();
            tmp.pop_back();
            ret.push_back(tmp);
            return;
        }

        if (root->left) dfs(root->left, tmp);
        if (root->right) dfs(root->right, tmp);
    }

};
class Solution
{
    int _k;
    int ret = 0;
public:
    int kthSmallest(TreeNode* root, int k)
    {
        _k = k;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if (root == nullptr) return;
        if (_k > 0) dfs(root->left);
        _k--;
        if (_k == 0) ret = root->val;
        if (_k > 0) dfs(root->right);
    }
};
class Solution
{
    long prev_min = LONG_MIN;
public:
    bool isValidBST(TreeNode* root)
    {
        return dfs(root);
    }
    bool dfs(TreeNode* root)
    {
        if (root == nullptr) return true;
        bool left = dfs(root->left);
        if (left == false) return false;
        if (prev_min >= root->val) return false;
        prev_min = root->val;
        bool right = dfs(root->right);
        if (right == false) return false;
        return true;
    }
};
class Solution
{
    int ret = 0;
public:
    int sumNumbers(TreeNode* root)
    {
        dfs(root, 0);
        return ret;
    }
    void dfs(TreeNode* root, int tmp)
    {
        tmp = tmp * 10 + root->val;
        if (root->left == nullptr && root->right == nullptr)
        {
            ret += tmp;
            return;
        }
        if (root->left) dfs(root->left, tmp);
        if (root->right) dfs(root->right, tmp);
    }
};
class Solution {
public:
    double myPow(double x, long long n)
    {
        if (x == 0) return 0;
        return n > 0 ? Pow(x, n) : 1.0 / Pow(x, -n);
    }
    double Pow(double x, long long n)
    {
        if (n == 0) return 1;
        double tmp = Pow(x, n / 2);
        return n % 2 == 1 ? tmp * tmp * x : tmp * tmp;
    }
};
class Solution {
public:
    ListNode* swapPairs(ListNode* head)
    {
        if (!head || !head->next) return head;
        ListNode* cur = head, * next = cur->next;
        ListNode* tmp = swapPairs(head->next->next);
        cur->next = tmp;
        next->next = cur;
        return next;
    }
};